<template>

  <div class="tools-div">
    <el-button
      type="success"
      size="small"
      @click=";(dialogFormVisible = true), (form = {})"
    >
      添 加
    </el-button>
  </div>

  <el-table :data="list" style="width: 100%">
    <!--    <el-table-column prop="categoryName" label="分类" />-->
    <el-table-column prop="name" label="品牌" />
    <el-table-column prop="logo" label="品牌图标" #default="scope">
      <img :src="scope.row.logo" width="50" />
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" />
    <el-table-column label="操作" align="center" width="200">
      <template #default="scope">
        <el-button
          type="primary"
          size="small"
          @click="handleEdit(scope.row), (dialogFormVisible = true)"
        >
          修改
        </el-button>
        <el-button type="danger" size="small" @click="handleDelete(scope.row),centerDialogVisible=true">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-pagination
    :page-sizes="[10, 20, 50, 100]"
    v-model:current-page="formInline.pageNum"
    v-model:page-size="formInline.pageSize"
    layout="total, sizes, prev, pager, next"
    :total="total"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  />
  <el-dialog v-model="centerDialogVisible" title="警告" width="30%" center>
    <span>
      确认删除
    </span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false,removeBrand()">
          删除
        </el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="dialogFormVisible" title="添加或修改品牌" width="50%">
    <el-form :model="form">
      <el-form-item label="品牌名称" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="logo" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :headers="headers"
        >
          <img v-if="form.logo" :src="form.logo" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click=";(dialogFormVisible = false), submitForm()"
        >
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import {addBrand, getBrandPage,modifyBrand,deleteBrand} from "@/api/brand"
import {baseURL} from "@/utils/request";
import {ElMessage} from "element-plus";
import {useApp} from "@/pinia/modules/app";


const centerDialogVisible = ref(false)
const dialogFormVisible = ref(false)
const formLabelWidth = '140px'
const uploadUrl = `${baseURL}/admin/uploadFile/logo`
//绑定token上传文件
const  headers = {
  token :useApp().authorization.token
}
// ================数据模型定义  start ===============================================
const form = ref({
  id : '',
  name: '',
  logo: '',
})
// 定义搜索表单数据模型
const brandList = ref([
])

const props = {
  lazy: true,
  value: 'id',
  label: 'name',
  leaf: 'leaf',
  lazyLoad(node, resolve) {   // 加载数据的方法
    const data = [
      {
        "id": 643,
        "createTime": "2023-05-22 15:31:18",
        "name": "玩具乐器",
        "imageUrl": "https://lilishop-oss.oss-cn-beijing.aliyuncs.com/0f423fb60f084b2caade164fae25a9a0.png",
        "parentId": 0,
        "status": 1,
        "orderNum": 10,
        "hasChildren": true,
        "children": null
      },
      {
        "id": 576,
        "createTime": "2023-05-22 15:31:13",
        "name": "汽车用品",
        "imageUrl": "https://lilishop-oss.oss-cn-beijing.aliyuncs.com/665dd952b54e4911b99b5e1eba4b164f.png",
        "parentId": 0,
        "status": 1,
        "orderNum": 10,
        "hasChildren": true,
        "children": null
      },
    ]
    resolve(data)  // 返回数据
  }
};
const categoryProps = ref(props)

// 定义表格数据模型
const list = ref([
  {
    "id": 2,
    "createTime": "2023-05-24 15:19:24",
    "brandId": 1,
    "categoryId": 99,
    "categoryName": "定制服务",
    "brandName": "小米",
    "logo": "http://139.198.127.41:9000/sph/20230506/小米.png"
  },
  {
    "id": 1,
    "createTime": "2023-05-06 10:59:08",
    "brandId": 2,
    "categoryId": 76,
    "categoryName": "UPS电源\t",
    "brandName": "华为",
    "logo": "http://139.198.127.41:9000/sph/20230506/华为.png"
  }
])

// 分页条数据模型
const total = ref(0)

//分页请求查询数据
const formInline = ref({
  pageNum: 1,
  pageSize: 10,
})

//======   数据模型定义end========================================================

const  handleSizeChange = (pageSize)=>{
  getPage()
}
const handleCurrentChange = (pageNum) =>{
  getPage()
}
//获取分页数据
const getPage = () =>{
  getBrandPage(formInline.value).then(res=>{
    list.value = res.data.list;
    total.value = res.data.total
    console.log(res)
  })
}

//上传成功后给logo地址赋值
const handleAvatarSuccess = (resp)=>{
  form.value.logo = resp.data
}
//上传前检查
const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('图片必须要jpg')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('图片不能超过2m')
    return false
  }
  return true
}
const handleEdit = (row)=>{
  form.value.id  = row.id
  form.value.logo = row.logo;
  form.value.name = row.name;
}
const handleDelete = async (row)=>{
  form.value.id = row.id
}
const removeBrand  = async ()=>{
  const {message} = await deleteBrand(form.value)
  ElMessage.success(message)
  getPage()
}

 const  submitForm =async ()=>{

  console.log(form.value.id);
  if (form.value.id =="" || form.value.id ==null){
   const {message} = await  addBrand(form.value);
   ElMessage.success(message)
   getPage()
  }else {
    //修改
    const {message} = await  modifyBrand(form.value);
    ElMessage.success(message)
    getPage()

  }
}

onMounted(()=>{
  getPage()
})
</script>
<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
  border: #ccc 1px dashed;
}
</style>